Set custom attribute value on cart page

In Magento 1.x

I have created following function in Namespace/Modulename/Model/Observer.php

public function salesQuoteItemSetCustomAttribute($observer){

        $quoteItem = $observer->getQuoteItem();
        $product = $observer->getProduct();
        $quoteItem->setCustomerProductPoints($product->getCustomerProductPoints());
    }

Called this function in Namespace/Modulename/etc/config.xml

    <sales_quote_item_set_product>
                <observers>
                    <product_point_quote>
                        <class>productpoint/observer</class>
                        <method>salesQuoteItemSetCustomAttribute</method>
                    </product_point_quote>
                </observers>
    </sales_quote_item_set_product>

Also Have converted my custom attribute quote to order and from order to quote by using following code in config.xml

    <sales_convert_quote_item>
        <customer_product_points>
            <to_order_item>*</to_order_item>
            <to_invoice_item>*</to_invoice_item>
            <to_shipment_item>*</to_shipment_item>
            <to_cm_item>*</to_cm_item>
        </customer_product_points>
    </sales_convert_quote_item>
    <sales_convert_order_item>
        <customer_product_points>
            <to_quote_item>*</to_quote_item>
            <to_invoice_item>*</to_invoice_item>
            <to_shipment_item>*</to_shipment_item>
            <to_cm_item>*</to_cm_item>
        </customer_product_points>
    </sales_convert_order_item>



In Magento 2

Change the following code:

Namespace/Modulename/Observer/salesQuoteItemSetCustomAttribute.php

<?php
namespace Namespace\Modulename\Observer;

use Magento\Framework\Event\ObserverInterface;

use Magento\Catalog\Model\Product;
use Magento\Checkout\Model\Cart;


class salesQuoteItemSetCustomAttribute implements ObserverInterface
{
   protected $_objectManager;

    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Catalog\Model\Product $product,
        \Magento\Framework\ObjectManagerInterface $interface,
        \Magento\Quote\Model\Quote\Item $quote
        \CollectionFactory $productCollectionFactory
    ) {
        $this->_objectManager = $objectManager;
        $this->cart = $cart;
        $this->product = $product;
        $this->objectManager = $interface;
        $this->quote = $quote;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $product = $observer->getProduct();
        $quoteItem = $observer->getQuoteItem();
        $quoteItem->setCustomAttribute($product->getCustomAttribute());
    }
}

etc/events.xml

<?xml version="1.0" encoding="UTF-8"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd">

            <event name="sales_quote_item_set_product">
                <observer
         name="product_point_quote"
         instance="Namespace\Modulename\Observer\salesQuoteItemSetCustomAttribute"/>
            </event>
</config>

Create catalog_attributes.xml in etc to convert attribute to quote:

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">
    <group name="quote_item">
        <attribute name="custom_attribute"/>
    </group>
</config>

This will bring your attribute on Cart Page


				

Leave a Comment